﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;

namespace ChangePasswordWebpart
{
    public partial class WPChangePassword_Control : System.Web.UI.UserControl
    {
        private const string DomainName = "";
        private string username = "";
        private string domain = "";
        private string ldapDomain = "";

        private void BindData()
        {
            SPWeb currentContext = SPControl.GetContextWeb(Context);
            string loginName = currentContext.CurrentUser.LoginName;
            
            if (string.IsNullOrEmpty(loginName))
            {
                LabelError.Text = "Error accessing login information.";
                LabelError.Visible = true;
                return;
            }

            if (loginName.Split('\\').Length != 2)
            {
                LabelError.Text = "Invalid login name format.";
                LabelError.Visible = true;
                return;
            }

            this.domain = loginName.Split('\\')[0];
            this.username = loginName.Split('\\')[1];
            this.ldapDomain = LDAPUtils.GetLdapDomainName(domain);

            if (string.IsNullOrEmpty(this.ldapDomain))
            {
                LabelError.Text = "Unable to retrieve LDAP domain.";
                LabelError.Visible = true;
                return;
            }

            LabelUsername.Text = String.Format(@"{0}\{1} ({2})", this.domain, this.username, this.ldapDomain);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            BindData();
        }

        protected void ButtonChange_Click(object sender, EventArgs e)
        {
            LabelError.Visible = false;
            LabelInfo.Visible = false;

            if ((TextBoxOldPassword.Text.Length == 0) || (TextBoxNewPassword.Text.Length == 0) || (TextBoxNewPasswordRepeat.Text.Length == 0))
            {
                LabelError.Text = "Please complete all fields.";
                LabelError.Visible = true;
                return;
            }

            if (TextBoxNewPassword.Text != TextBoxNewPasswordRepeat.Text)
            {
                LabelError.Text = "New passwords must match.";
                LabelError.Visible = true;
                return;
            }

            string error = string.Empty;
            if (!LDAPUtils.Authenticate(this.username, TextBoxOldPassword.Text, this.ldapDomain, out error))
            {
                LabelError.Text = String.Format("Old password is incorrect. {0}", error);
                LabelError.Visible = true;
                return;
            }

            if (!LDAPUtils.ChangePassword(this.username, TextBoxOldPassword.Text, TextBoxNewPassword.Text, this.ldapDomain, out error))
            {
                LabelError.Text = String.Format("Unable to change password. {0}", error);
                LabelError.Visible = true;
                return;
            }
            else
            {
                LabelInfo.Text = "Password Changed.";
                LabelInfo.Visible = true;
            }
        }
    }
}